package com.century.sjt.activity.Topic;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.century.sjt.R;
import com.century.sjt.activity.BaseActivity;
import com.century.sjt.fragment.MineTabSquareFragment;
import com.century.sjt.fragment.SocialTabSquareFragment;
import com.century.sjt.widget.TitleBar.SjtTitleBar;

import java.util.ArrayList;
import java.util.List;


public class MySocialZoneNewActivity extends BaseActivity {

    private SjtTitleBar titleBar;


    //tab 开始
    private ViewPager mViewPager;
    private FragmentPagerAdapter mAdapter;
    private List<Fragment> mDatas;

    private TextView mChatTextView;
    private TextView mFriendTextView;
    private TextView mContactTextView;
    private LinearLayout mChatLinearLayout;

    private ImageView mTabline;
    private int mScreen1_3;

    private int mCurrentPageIndex;

    private LinearLayout mTab1,mTab2,mTab3;
    //tab 结束


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_social_zone_new);

        titleBar = (SjtTitleBar) findViewById(R.id.title_bar);
        //       标题栏
        titleBar.setRightIconResourc(getResources().getString(R.string.icon_write));
        titleBar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        titleBar.setRightLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MySocialZoneNewActivity.this, MyPublishTopicActivity.class);
                intent.putExtra("content", "");
                startActivity(intent);
                overridePendingTransition(R.animator.in_from_right, R.animator.out_to_left);
            }
        });
        titleBar.setTitle(getResources().getString(R.string.tab_friends_zone));


        initTabLine();
        initView();
    }

    private void initTabLine()
    {
        mTabline = (ImageView) findViewById(R.id.id_iv_tabline);
        Display display = getWindow().getWindowManager().getDefaultDisplay();
        DisplayMetrics outMetrics = new DisplayMetrics();
        display.getMetrics(outMetrics);
        mScreen1_3 = outMetrics.widthPixels / 2;
        ViewGroup.LayoutParams lp = mTabline.getLayoutParams();
        lp.width = mScreen1_3;
        mTabline.setLayoutParams(lp);
    }
    @Override
    protected void initView()
    {

        mViewPager = (ViewPager) findViewById(R.id.my_social_zone_new_myviewpager);
        mChatTextView = (TextView) findViewById(R.id.my_social_zone_new_guangchang);
        mFriendTextView = (TextView) findViewById(R.id.my_social_zone_new_mine);
//        mContactTextView = (TextView) findViewById(R.id.my_social_zone_new_contact);

        mTab1 = (LinearLayout) findViewById(R.id.my_social_zone_new_ll_tab1);
        mTab2 = (LinearLayout) findViewById(R.id.my_social_zone_new_ll_tab2);
//        mTab3 = (LinearLayout) findViewById(R.id.my_social_zone_new_ll_tab3);

        mDatas = new ArrayList<Fragment>();

        SocialTabSquareFragment tab01 = new SocialTabSquareFragment();
        MineTabSquareFragment tab02 = new MineTabSquareFragment();
//        MineTabSquareFragment tab03 = new MineTabSquareFragment();

        mDatas.add(tab01);
        mDatas.add(tab02);
//        mDatas.add(tab03);




        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager())
        {
            @Override
            public int getCount()
            {
                return mDatas.size();
            }

            @Override
            public Fragment getItem(int arg0)
            {
                return mDatas.get(arg0);
            }
        };
        mViewPager.setAdapter(mAdapter);


        mTab1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resetTextView();
                mChatTextView.setTextColor(getResources().getColor(R.color.textdefault_title));
//                mTab1.setBackgroundColor(getResources().getColor(R.color.textdefault_title));
                mViewPager.setCurrentItem(0);
                mTabline.startAnimation(AnimationUtils.loadAnimation(MySocialZoneNewActivity.this, R.anim.scale));

            }
        });


        mTab2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resetTextView();
                mFriendTextView.setTextColor(getResources().getColor(R.color.textdefault_title));
//                mTab2.setBackgroundColor(getResources().getColor(R.color.white));
                mViewPager.setCurrentItem(1);
                mTabline.startAnimation(AnimationUtils.loadAnimation(MySocialZoneNewActivity.this, R.anim.scale));

            }
        });

        intoVP();

    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {

    }

    private void intoVP() {
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
        {
            @Override
            public void onPageSelected(int position)
            {
                resetTextView();
                switch (position)
                {
                    case 0:
//					if (mBadgeView != null)
//					{
//						mChatLinearLayout.removeView(mBadgeView);
//					}
//					mBadgeView = new BadgeView(MainActivity.this);
//					mBadgeView.setBadgeCount(7);
//					mChatLinearLayout.addView(mBadgeView);

//                        mChatTextView.setTextColor(getResources().getColor(R.color.titlered));
                        break;
                    case 1:
//                        mFriendTextView.setTextColor(getResources().getColor(R.color.titlered));
                        break;
//                    case 2:
//                        mContactTextView.setTextColor(Color.parseColor("#008000"));
//                        break;

                }

                mCurrentPageIndex = position;

            }

            @Override
            public void onPageScrolled(int position, float positionOffset,
                                       int positionOffsetPx)
            {
//                Log.e("TAG", position + " , " + positionOffset + " , "+ positionOffsetPx);

                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mTabline
                        .getLayoutParams();

                if (mCurrentPageIndex == 0 && position == 0)// 0->1
                {
                    lp.leftMargin = (int) (positionOffset * mScreen1_3 + mCurrentPageIndex
                            * mScreen1_3);
                } else if (mCurrentPageIndex == 1 && position == 0)// 1->0
                {
                    lp.leftMargin = (int) (mCurrentPageIndex * mScreen1_3 + (positionOffset - 1)
                            * mScreen1_3);
                }
//                else if (mCurrentPageIndex == 1 && position == 1) // 1->2
//                {
//                    lp.leftMargin = (int) (mCurrentPageIndex * mScreen1_3 + positionOffset
//                            * mScreen1_3);
//                } else if (mCurrentPageIndex == 2 && position == 1) // 2->1
//                {
//                    lp.leftMargin = (int) (mCurrentPageIndex * mScreen1_3 + ( positionOffset-1)
//                            * mScreen1_3);
//                }
                mTabline.setLayoutParams(lp);

            }

            @Override
            public void onPageScrollStateChanged(int arg0)
            {
                // TODO Auto-generated method stub

            }
        });
    }

    protected void resetTextView()
    {
        mChatTextView.setTextColor(getResources().getColor(R.color.textdefault_title));
        mFriendTextView.setTextColor(getResources().getColor(R.color.textdefault_title));
//        mContactTextView.setTextColor(Color.BLACK);
    }


}
